﻿package domino 
{
	/**
	 * ...
	 * @author sergiy
	 */
  import Box2D.Dynamics.b2World;
  import flash.display.DisplayObjectContainer;
  import flash.display.*;
  import flash.events.*;
  import flash.net.URLRequest;
  import flash.net.navigateToURL;
  import flash.text.*;
  import mochi.as3.MochiScores;
  import org.as3lib.kitchensync.*;
    import org.as3lib.kitchensync.action.*;
		import flash.utils.Timer;
  
	 public class mainMenu extends EventDispatcher
	{
		[Embed(source="img/figureZone.svg")]
           public var figureZoneSVG:Class;
		   
		[Embed(source="img/logo.svg")]
           public var logoSVG:Class;  
		   
		[Embed(source="img/level.svg")]
           public var levell:Class;
		   
		[Embed(source="img/levelu.svg")]
           public var levelu:Class; 
		 
		[Embed(source="img/levelun.svg")]
           public var levelun:Class;    
		
		[Embed(source="img/leveln.svg")]
           public var leveln:Class; 
		   
		[Embed(source="img/levelbign.svg")]
           public var levelbign:Class;
		 
		[Embed(source="img/levelbig.svg")]
           public var levellbig:Class;
		   
		[Embed(source="img/levelubig.svg")]
           public var levelubig:Class;
		      
		[Embed(source="img/levelubign.svg")]
           public var levelubign:Class; 
		
		   [Embed(source="img/HoboStd.otf" , fontName = "HoboFam",mimeType="application/x-font-truetype")]
           public   static const  HoboFo:String;   
		//[Embed(source="img/HoboStd.otf")]
          // public var Hobo:Class;
		   
		
		
		public var start_button:start2= new start2();
		private var menu:Sprite = new Sprite;
		private var a:Array = new Array();
		private var b:Array = new Array();
		private var c:Array = new Array();
		private var ABC:Array = new Array();
		private var scoreArray:Array = new Array();
		
		private var parent:DisplayObjectContainer;
		public var main_level:int;
		private var level:TextField = new TextField();
		private var world:b2World;
		private var brun:SimpleButton = new Run();
		public var main_menu:Menu = new Menu();
		public var main_restart:Restart = new Restart();
		public var walk:Walkaround = new Walkaround();
		public var submit:highscore = new highscore();
		public var replay:Restart = new Restart();
		public var locked:Array = new Array();
		public var passed:Array = new Array();
		
		
		private var main_int:Sprite = new Sprite();
		private var right_menu:Sprite = new Sprite();
		private var next:Next = new Next();
		private var done:welldone = new welldone();
		
		private var format1:TextFormat = new TextFormat();
		private var format2:TextFormat = new TextFormat();
		private var format3:TextFormat = new TextFormat();
		private var format4:TextFormat = new TextFormat();
		
		private	var level_int:TextField = new TextField();
		private	var tut:Array = new Array();
		public	var scores:TextField = new TextField();
		public	var scores2:TextField = new TextField();
		public	var times:TextField = new TextField();
		
		public var target_coin:TextField = new TextField();
		public var total:TextField = new TextField();
		public var total3:TextField = new TextField();
		
		public var total2:TextField = new TextField();
		public var clicks:TextField = new TextField();
		public var clicks2:TextField = new TextField();
		public var clickNumber:int =-1;
		public var coin:TextField = new TextField();
		public var coin2:TextField = new TextField();
		public var failed:Failed = new Failed();
		
		public var coins:int=0;
		public var target:int =0;
		public var _dominArray:Array ;
		public var levels2:levels;
        public var current_scores:Number=1000;
		public var common_scores:Number=0;
		public var levels_scores:Array = [];
		public var levels_access:Array = [];
		
		public var timer:Timer = new Timer(200, current_scores);
		public var fZone:Sprite = new figureZoneSVG();
		public var soundZone:zoneSound = new zoneSound();
		private var logotip:Sprite = new logoSVG();
		
		
		private var sprite:TextField = new TextField(); 
		private var title2:TextField = new TextField(); 
		public var music:sounds;
		
		public var fps:TextField = new TextField(); 
		
		
		//public var timer:Timer = new Timer(1000,1000);
		
		public function mainMenu(_parent:DisplayObjectContainer,_world:b2World,a:Array,scoreA:Array) 
		{
			
			scoreArray = scoreA;
			parent = _parent;
			world = _world;
		    levels_access = a;
			parent.addChild(title2);
			title2.visible = true;
			AddMainMenu();
			gameInterface();
			nextLevelForm();
			addlevelButton();
			addSoundZone();
			hideInterface();
			viewMainMenu();
			hideLevels();
			
			 music.PlaySound("loop");	
		}//end
		
		private function addSoundZone():void{

			soundZone.getChildByName("bsound").addEventListener(MouseEvent.CLICK, soundListener);
			music = new sounds();
		   
			//soundZone.getChildByName("bmusic").addEventListener(MouseEvent.CLICK, musicListener);
		
			soundZone.y = 348;
            soundZone.x = 595;
 
			parent.addChild(soundZone);
		}
		
		private function musicListener(e:MouseEvent):void 
		{
			trace("music");
		}
		
		private function soundListener(e:MouseEvent):void 
		{
			music.PlaySound("button");
			if (music.sound_inited == true){
			 music.PlaySound("stop");
			 music.sound_inited = false;
			}
			else {
			    music.sound_inited = true;
				music.PlaySound("start"); 	
			  
			}
			trace("sound");
		}

		
		private function scoreHandler(e:TimerEvent):void 
		{
			scores.text =  "SCORES "  + String(0);
			
			//trace("time elapsed");
		}//end
		private function AddMainMenu():void {
		    start_button.x = 20;
			start_button.y = 20;
			start_button.addEventListener(MouseEvent.CLICK, startHandler);
		    menu.addChild(start_button);
			menu.x = 300;
			menu.y = 100;
			parent.addChild(menu);
		 }//end

		private function startHandler(event:MouseEvent):void {
		  music.PlaySound("button");
		  trace("start");	
		  viewLevels();
		  HideMainMenu();		  
	     }//end
      
		public function viewMainMenu():void {
		  menu.visible = true;
		 }//end
		 
		public function viewSoundZone():void {
		  soundZone.visible = true;
		 }//end
		
		 public function hideSoundZone():void {
		  soundZone.visible = false;
		 }//end	
		
		 
		 public function reloadScores(scoreArray:Array):void {
			 
		  for (var i:int = 0; i < 30; i++ ) {
		   c[i].text = String(scoreArray[i]);
			  }
			 
		 }//end
		 public function reloadScoresArray(A:Array):void {
		 scoreArray = A;
		 }//end
		 
		 
		public function HideMainMenu():void {
		  menu.visible = false;
		 }//end
		
		public function setArrayAccess(a:Array):void {
		  levels_access = a;
		 }//end
		
		private function addlevelButton():void {
		
		
			  
			
			
		for (var i:int =0; i < 30; i++ ){
			
			var lb2:LevButton = new LevButton();
				
               
			   
           		passed.push(new Passed());
				locked.push(new Locked());
				passed[i].visible = false;
			if (levels_access[i] == false) {

			    lb2.upState = new levell(); 
				lb2.overState = new levellbig(); 
				lb2.downState = new levellbig();
				lb2.hitTestState = new levellbig();
				a.push(lb2);
			    passed[i].visible = false;
				locked[i].visible = true;
			}
			else 
			if (scoreArray[i] != 0) {
				lb2.upState = new levelu(); 
				lb2.overState = new levelubig(); 
				lb2.downState = new levelubig();
				lb2.hitTestState = new levelubig();
				a.push(lb2);
			    passed[i].visible = true;
			    locked[i].visible = false;	
			}
			else {
				lb2.upState = new levelu(); 
				lb2.overState = new levelubig(); 
				lb2.downState = new levelubig();
				lb2.hitTestState = new levelubig();
				a.push(lb2);
				
			    passed[i].visible = false;
			    locked[i].visible = false;
			}
			
			 b.push(new TextField());
			 c.push(new TextField());
			// passed.push(new Passed());
			 //locked.push(new Locked());
			 
			 
			ABC.push(new Sprite());
			
			b[i].text = String(i);
			if(i<10){
			 b[i].x = 20 + int(i % 6) * 80;
			 b[i].y = 14 + int(i/6) * 80;
			}
			else
			{
			 b[i].x = 10 + int(i % 6) * 80;
			 b[i].y = 13 + int(i/6) * 80;
			}
			b[i].selectable = false;
			b[i].mouseEnabled = false;
			b[i].textColor = 0xFF0000;
			b[i].alpha = 0.5;
			b[i].embedFonts = true;
			b[i].setTextFormat(format3);
			
			a[i].x = int(i%6) * 80;
			a[i].y = int(i / 6) * 80;
			a[i].text =String(i);
			
			if(levels_access[i]==true) 
			 a[i].addEventListener(MouseEvent.CLICK, levelHandler);
			else{
			}
			c[i].x =  15+int(i % 6) * 80;
			c[i].y = 62 + int(i/6) * 80;
			c[i].text = "0";
			c[i].selectable = false;
			c[i].embedFonts = true;
			c[i].defaultTextFormat = format2;
			
			
			passed[i].x = 32+int(i % 6) * 80;
			passed[i].y = 34 + int(i/6) * 80;
			passed[i].text = "0";
			passed[i].selectable = false;
		//	passed[i].visible = false;
		
	//	trace(levels_scores[i]);
		//if (levels_scores[i] > 0){
		 //trace(levels_scores[i]);
		 //passed[i].visible = "0";
		//}
		
			passed[i].mouseEnabled = false;
			
			//passed[i].defaultTextFormat = format2;
			
		    locked[i].x = 32+int(i % 6) * 80;
			locked[i].y = 34 + int(i/6) * 80;
			locked[i].text = "0";
			locked[i].selectable = false;
		    locked[i].mouseEnabled = false;
			//locked[i].defaultTextFormat = format2;
			
			 
			
			ABC[i].x = 80 + int(i % 6);
			ABC[i].y = 36 + int(i/6);
			
			  ABC[i].addChild(a[i]);
			  ABC[i].addChild(c[i]);
			  ABC[i].addChild(b[i]);
			  ABC[i].addChild(passed[i]);
			  ABC[i].addChild(locked[i]);
			  
			  parent.addChild(ABC[i]);
			  parent.addChild(total2);
		  
		  
		}
		   total2.x = 160;
		   total2.y = 430;
		   total2.width = 400;
		   total2.defaultTextFormat = format3;
		   total2.embedFonts = true;
		   total2.selectable = false;
		   total2.type = TextFieldType.DYNAMIC;
		   total2.text = "TOTAL SCORES " + String(common_scores-int(c[main_level].text));
		   right_menu.addChild(total2); 
		   parent.addChild(total2);
		   
		   submit.x = 560;
		   submit.y = 460;
		   submit.addEventListener(MouseEvent.CLICK, submitHandler);
		 parent.addChild(submit);
		 
		 
		        tut.push(new tutorial());
				tut.push(new tutorial());
			
				 tut[0].x = 109;
			     tut[0].y = 72;
			     tut[0].selectable = false;
		         tut[0].mouseEnabled = false;
				 tut[0].visible = false;
				 
				 parent.addChild(tut[0]);
			
			     tut[1].x = 189;
			     tut[1].y = 72;
			     tut[1].selectable = false;
		         tut[1].mouseEnabled = false;
				 tut[1].visible = false;
				 
			     parent.addChild(tut[1]);
				 
				 
		   
		   
		   
		  
		}//end
     public function hideLevels():void {
		 for (var i:int = 0; i < 30; i++ ) {
				ABC[i].visible = false;
				}
		submit.visible = false;		
		total2.visible = false;		
		title2.visible = false;
		tut[1].visible = false;
		tut[0].visible = false;
		
		//tut.visible = false;
		}//end

     private function viewLevels():void {
		parent.dispatchEvent(new menuEvents(menuEvents.LEVEL_VIEW));
		 for (var i:int = 0; i < 30; i++ ) {
				ABC[i].visible = true;
				
				}
		submit.visible = true;	
		total2.visible = true;	
		title2.visible = true;
		trace("scoreArray[0]" + scoreArray[0]);
		if(scoreArray[0]==0)
		 tut[0].visible = true;
		if(scoreArray[1]==0)
		 tut[1].visible = true;
		
		 }//end 
		
	private function levelHandler(event:MouseEvent):void {
	//	if()
	             music.PlaySound("button");			
	            // trace(event.target+ "uuooooooooooooooooo");
	             main_level = int(event.currentTarget.text);
				 level_int.text ="LEVEL " + String(main_level);
				 hideLevels();
				 
				 viewInterface();
				 viewSoundZone();
				 
			    levels2 = new levels(main_level, world, parent);
				levels2.Load();
				//clickNumber = levels2.clicks;
				//trace(clickNumber +" = clickNumber33");
				if(timer)
				 timer.reset();
				 current_scores = 1000;
				/* timer = new Timer(1000, 1000);
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE);
			    timer.removeEventListener(TimerEvent.TIMER);
			     */
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, scoreHandler);
			    timer.addEventListener(TimerEvent.TIMER, timeHandler);
			    timer.start();
				trace(timer.start+ "timer is started");
				title2.visible = false;
				
				 
			}//end
	
	      public function addScores(s:Number):void{
				common_scores = s;
			}
			
	private function timeHandler(e:TimerEvent):void {
			//trace("time");
			if(current_scores>0)
			 current_scores--;
			//else
			 //current_scores=0;
			scores.text= "SCORES "  + String(current_scores);
			total.text = "TOTAL " + String(int(current_scores-int(c[main_level].text))+common_scores);
		   }//end
			
			
	public function nextLevelForm():void {
		
		    
			 
		 	next.x = 320;
			next.y = 160;
			next.visible = false;
			next.name = "next";
			next.getChildByName("NEXT_LEVEL").addEventListener(MouseEvent.CLICK, nextLevel);
			next.getChildByName("REPLAY").addEventListener(MouseEvent.CLICK, restartHandler);
			
			failed.x = 320;
			failed.y = 160;
			failed.visible = false;
			failed.name = "failed";
			failed.getChildByName("REPLAY").addEventListener(MouseEvent.CLICK, restartHandler);
		    failed.getChildByName("WALK").addEventListener(MouseEvent.CLICK, helpHandler);
			
			done.x = 320;
			done.y = 160;
			done.visible = false;
			done.name = "done";
			//done.addChild(total);
			
			total3.x = -50;
		    total3.y = 14;
			total3.defaultTextFormat=format4;
			total3.selectable = false;
			total3.embedFonts = true;
		    total3.type = TextFieldType.DYNAMIC;
			total3.width = 300;
		    total3.text = "TOTAL " + String(common_scores);
			total3.visible = true;
			done.addChildAt(total3,2);
			
			done.getChildByName("REPLAY2").addEventListener(MouseEvent.CLICK, restartHandler);
		    done.getChildByName("LEVELS").addEventListener(MouseEvent.CLICK, bmenuHandler);
			done.getChildByName("SUBMIT").addEventListener(MouseEvent.CLICK, submitHandler);
		    
			
			
			
			times.x = -60;
		    times.y = 25;
			times.width = 200;
			times.defaultTextFormat=format4;
		    times.selectable = false;
			times.embedFonts = true;
			times.type = TextFieldType.DYNAMIC;
		    times.text = "TIME "  + String(1000-timer.currentCount);		
		    next.addChild(times);
			
			scores2.x = -60;
		    scores2.y = 88;
			scores2.width = 200;
			scores2.defaultTextFormat = format4;
			scores2.embedFonts = true;
		    scores2.selectable = false;
			scores2.type = TextFieldType.DYNAMIC;
		    scores2.text = "scores "  + String(current_scores);		
		    next.addChild(scores2);
			 
			coin2.x = -60;
		    coin2.y = 45;
			coin2.width = 200;	
			coin2.defaultTextFormat=format4;
		    coin2.selectable = false;
			coin2.embedFonts = true;
		    
			coin2.type = TextFieldType.DYNAMIC;
		    coin2.text = "STARS "  + String(coins)+ "x100";		
		    next.addChild(coin2);
			
			clicks2.x = -60;
		    clicks2.y = 65;
			clicks2.width = 200;
			clicks2.defaultTextFormat=format4;
		    clicks2.selectable = false;
			clicks2.embedFonts = true;
			clicks2.type = TextFieldType.DYNAMIC;
		    clicks2.text = "CLICKS "  + String(clickNumber)+"x500";		
		    next.addChild(clicks2);
			
			//main_restart.addEventListener(MouseEvent.CLICK, restartHandler);
			
			//next.getChildByName("NEXT_LEVEL").addEventListener(MouseEvent.CLICK, nextLevel);
		}//end
		
		private function submitHandler(e:MouseEvent):void 
		{
			
		
        var o:Object = { n: [12, 11, 3, 4, 3, 7, 4, 15, 1, 11, 2, 8, 9, 2, 15, 9], f: function (i:Number,s:String):String { if (s.length == 16) return s; return this.f(i+1,s + this.n[i].toString(16));}};
        var boardID:String = o.f(0,"");
        MochiScores.showLeaderboard({boardID: boardID, score: common_scores});
		
		//MochiScores(
			//MochiScores.showLeaderboard({boardID: ”Highscores”});
			//MochiScores.showLeaderboard({boardID:"e8f1fb7c28f77337", score:common_scores,onClose:function ():void });
		//	MochiScores.showLeaderboard({boardID: "e8f1fb7c28f77337", res: "550x400", clip: this, score:common_scores,onClose:function ():void});
			//MochiScores.showLeaderboard({boardID:"Highscores", score:common_scores});
			//trace("submit");
		}
		
     public function gameInterface():void {
			
		     
		     format1.font= "HoboFam"; 
             format1.color = 0x996600;
			 format1.bold = true;
			 format1.size = 12;
			 
			 
			 format2.font="HoboFam"//new HoboFo().fontName;
             format2.color = 0x996600;
			 format2.bold = true;
			 format2.size = 12;
			 
			 format3.font="HoboFam"//new HoboFo().fontName;
             format3.color = 0x996600;
			 format3.bold = true;
			 format3.size = 30;
			 
			 format4.font="HoboFam"//new HoboFo().fontName;
             format4.color = 0x996600;
			 format4.bold = true;
			 format4.size = 18;
			 
			title2.defaultTextFormat = format3;
            title2.maxChars = 20;
			title2.width = 280;
			title2.text = "CATCH THE STAR";//*/
			title2.x = 170;
			title2.y = -6;
			title2.alpha = 1;
			title2.embedFonts = true;
			title2.mouseEnabled = false;
			
            //next
			
			//
			
			fps.x = 10; 
		    fps.y = 10;
		//	parent.addChild(fps);
			
			fZone.x = 552;
			fZone.y = 16;
			parent.addChild(fZone);
			//fZone.addEventListener(MouseEvent.CLICK, brunHandler);
			
	        //redactor buttons
	      /*  brun.x = 150;
			brun.y = -40;
			main_int.addChild(brun);
			brun.addEventListener(MouseEvent.CLICK, brunHandler);*/
			
			main_restart.x = 152;
			main_restart.y = -11;
			main_int.addChild(main_restart);
			main_restart.addEventListener(MouseEvent.CLICK, restartHandler);
			
			walk.x = 152;
			walk.y = 34;
			main_int.addChild(walk);
			walk.addEventListener(MouseEvent.CLICK, helpHandler);

			
			replay.x = 302;
			replay.y = 200;
			//parent.addChild(replay);
			replay.addEventListener(MouseEvent.CLICK, restartHandler);
			replay.name = "replay";
			//replay.visible = false;
			
			main_menu.x = 154;
			main_menu.y = 14;
			main_int.addChild(main_menu);
			main_menu.addEventListener(MouseEvent.CLICK, bmenuHandler);
			
			main_int.x = 445;
			main_int.y = 430;
			parent.addChild(main_int);
			
		//	right menu
			level_int.x = -300;
		    level_int.y = 20;
			level_int.width = 350;
			level_int.embedFonts = true;
			level_int.defaultTextFormat = format4;
			level_int.selectable = false;
			level_int.alpha = 0.8;
			
			
			
			
			level_int.type = TextFieldType.DYNAMIC;
		    level_int.text ="LEVEL " +  String(main_level);
		    right_menu.addChild(level_int);
			
			
			target_coin.x = 0;
		    target_coin.y = 274;
			target_coin.defaultTextFormat=format1;
			target_coin.selectable = false;
			target_coin.embedFonts = true;
			target_coin.type = TextFieldType.DYNAMIC;
		    target_coin.text = "TARGET " + String(target);
		//	right_menu.addChild(target_coin);
		
		    total.x = 0;
		    total.y = 330;
			total.defaultTextFormat = format1;
			total.embedFonts = true;
			total.selectable = false;
		    total.type = TextFieldType.DYNAMIC;
		    total.text = "TOTAL " + String(common_scores);
			right_menu.addChild(total);
			
			
		
			coin.x = 0;
		    coin.y = 290;
			coin.defaultTextFormat=format1;
		    coin.selectable = false;
			coin.embedFonts = true;
			coin.type = TextFieldType.DYNAMIC;
		    coin.text = "STARS "  + String(coins);
			parent.addEventListener(eventLoadLevel.DELETE_COIN, getCoin);
			parent.addEventListener(eventLoadLevel.DELETE_COIN_BLACK, getCoinBlack);
		  //  right_menu.addChild(coin);
			
			logotip.x = 0;
		    logotip.y = 338;
			//right_menu.addChild(logotip);
			
			clicks.x = 0;
		    clicks.y = 290;
			clicks.defaultTextFormat = format1;
			clicks.embedFonts = true;
			clicks.selectable = false;
			clicks.type = TextFieldType.DYNAMIC;
		    clicks.text = "CLICKS " + String(clickNumber);
			right_menu.addChild(clicks);
		
			scores.x = 0;
		    scores.y =310
			scores.defaultTextFormat=format1;
			scores.selectable = false;
			scores.embedFonts = true;
			scores.type = TextFieldType.DYNAMIC;
		    scores.text = "SCORES "  + String(current_scores);
			right_menu.addChild(scores);
			
			/*tut.x = 120;
		    tut.y = 80;
			//tut.width = 200;
			//tut.alpha = 0.8;
			//tut.rotation = 30;
			tut.defaultTextFormat=format1;
			tut.selectable = false;
		    tut.mouseEnabled = false;
			//tut.text = "TUTORIAL LEVEL";
			tut.visible = false;
			//parent.addChild(tut);*/
			
			
			right_menu.x = 555;
			right_menu.y = -20;
			parent.addChild(right_menu);
			
			
		}//end
		
		public function setLevelAccess(i:int):void {
			
			if (i > 29)
			 return;
			 
		   //  ABC[i].removeChild(a[i]);
			// ABC[i].removeChild(b[i]);
			 
			/*a[i] = new LevButton();
			
				a[i].upState = new levelun(); 
				a[i].overState = new levelubign(); 
				a[i].downState = new levelubign();
				a[i].hitTestState = new levelubign();
				*/
			trace("HUUUUUUUUUUUUUUUUU");
			a[i].x = int(i%6) * 80;
			a[i].y = int(i / 6) * 80;
			
			a[i].text = String(i);
			a[i].visible = true;
            a[i].addEventListener(MouseEvent.CLICK, levelHandler);
			 
			ABC[i].removeChild(passed[i]);
			ABC[i].removeChild(locked[i]);
			//ABC[i].removeChild(tut[1]);
			
			ABC[i].addChild(passed[i]);
			ABC[i].addChild(locked[i]);
			//ABC[i].addChild(tut[i]);
			
			passed[i].visible = true;
			
			locked[i].visible = false;
			//tut[i].visible = false;
			
			 
		 //ABC[i].addChild(a[i]);
		// ABC[i].addChild( b[i]);
			 
			}//end
			
			
		public function unlockLevel(i:int):void {
			
			
			 
		     ABC[i].removeChild(a[i]);
			 ABC[i].removeChild(b[i]);
			 
			a[i] = new LevButton();
			
				a[i].upState = new levelu(); 
				a[i].overState = new levelubig(); 
				a[i].downState = new levelubig();
				a[i].hitTestState = new levelubig();
				
			
			//passed[i].visible = false;
			locked[i].visible = false;
			a[i].x = int(i%6) * 80;
			a[i].y = int(i / 6) * 80;
			
			a[i].text = String(i);
			a[i].visible = true;
            a[i].addEventListener(MouseEvent.CLICK, levelHandler);
			 
			 
			 
			ABC[i].addChild(a[i]);
		    ABC[i].addChild( b[i]);
			 
			}//end	
		private function getCoin(e:eventLoadLevel):void
		{
			coins++;
			
			timer.repeatCount += 100;
			current_scores += 100;
			coin.text = "STARS " + String(coins);
			scores.text = "SCORES "  + String(current_scores);
			music.PlaySound("star");
			
			if (levels2.getCoins() <= 0)
	 		{
				if (main_level == 29){
				  parent.addChild(done);
				  coin2.text = "STARS " + String(coins)+"x100";
				  clicks2.text =  "CLICKS " + String(clickNumber)+ "x500";
				  scores2.text =  "SCORES "  + String(current_scores + clickNumber * 500);
				  total.text = "TOTAL " +String(current_scores + clickNumber * 500 + common_scores-int(c[main_level].text));
				  times.text = "TIME " + String(1000 - timer.currentCount);
				  done.visible = true; 
				  total3.text = total.text;
				  trace(times.text);
				  trace("main_level");
			 	  parent.dispatchEvent(new eventRun(eventRun.STOP_GAME));
				  music.PlaySound("complete");
				  }
				 else {
				parent.addChild(next);
				  coin2.text = "STARS " + String(coins)+"x100";
				  clicks2.text =  "CLICKS " + String(clickNumber)+ "x500";
				  scores2.text =  "SCORES "  + String(current_scores + clickNumber * 500);
				  total.text = "TOTAL " +String(current_scores + clickNumber * 500 + common_scores);
				  
				  times.text = "TIME " + String(1000 - timer.currentCount);
				  next.visible = true; 
			 	  parent.dispatchEvent(new eventRun(eventRun.STOP_GAME));
				  music.PlaySound("complete");
				 }
			}
	
		}
		
		private function getCoinBlack(e:eventLoadLevel):void
		{
				   trace("BLACK is here");
				   //parent.addChild(next);
				  // next.visible = true;
				   parent.addChild(failed);
				   failed.visible = true;
				   
				   
			 	   parent.dispatchEvent(new eventRun(eventRun.GAME_OVER));
				   music.PlaySound("fail");
				   //restartHandler(null);
				  // run = false;
			
		}
		
		private function helpHandler(e:MouseEvent):void 
		{
			var url:String="http://www.youtube.com/watch?v=SJsNs8cBNWI";
            var urlRequest2:URLRequest=new URLRequest(url);
            navigateToURL(urlRequest2);
			trace("Walkthroat");
		}//end
		public function nextLevel(e:Event):void
		{
			trace(current_scores);
			selectNextLevel();
			
			 
				level_int.text = "LEVEL " + String(main_level);		
    		//	new KSDispatchEvent("clearLevel", this, 2,testListener);
	        	parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.CLEAR_LEVEL));
				hideLevels();
				parent.removeChild(next);
				viewInterface();
			    levels2 = new levels(main_level, world, parent);
				levels2.Load();	
				coins = 0;
				clickNumber = levels2.clicks; 
				trace("NEXT LEVEL");
				timer.reset();
				current_scores = 1000;
				
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, scoreHandler);
			    timer.addEventListener(TimerEvent.TIMER, timeHandler);
			    timer.start();
				//next.visible = true;	
		}//end
		
		private function selectNextLevel():void
		{

			
			if ((scoreArray[main_level + 1] == 0) && (levels_access[main_level + 1] == true)) {
				
			main_level++;
			//return main_level;
			}
			else { 
				 if (main_level == 29){
			      main_level = 0;
			      selectNextLevel();
			  }
			     else
				 {
			         main_level++;
					 selectNextLevel();
				 }
			}
			
		}//end
		
		private function testListener():void
		{
			trace("Clear all");
		}//end
		
		public function restartHandler(e:Event):void 
		{
		    music.PlaySound("button");
			parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.CLEAR_LEVEL));
			
			if(parent.getChildByName("next"))
			 parent.removeChild(next);
            
			if(parent.getChildByName("failed"))
			 parent.removeChild(failed);
			 
			if(parent.getChildByName("done"))
			 parent.removeChild(done);
             
			if (parent.getChildByName("replay"))
			 parent.removeChild(replay);
            
			 
			// parent.
			(levels2 = new levels(main_level, world, parent) ).Load();
			clickNumber = levels2.clicks;
			coins = 0;
			clicks.text = "CLICKS " + String(clickNumber);
			trace("Restart");
			timer.reset();
			current_scores = 1000;
		  
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, scoreHandler);
			timer.addEventListener(TimerEvent.TIMER, timeHandler);
			timer.start();
		}
		
		private function bmenuHandler(e:Event):void 
		{
			music.PlaySound("button");
			viewLevels();
			total2.text = "TOTAL SCORES " + String(common_scores);
			hideInterface();
			//viewMainMenu();
			//removeDomino
			if(parent.getChildByName("next"))
			 parent.removeChild(next);
			if(parent.getChildByName("done"))
			 parent.removeChild(done);
			
			if(parent.getChildByName("failed"))
			 parent.removeChild(failed);
			  
			if (parent.getChildByName("replay"))
			 parent.removeChild(replay);
			 
			parent.dispatchEvent(new eventLoadLevel(eventLoadLevel.CLEAR_LEVEL));
			trace("Menu");
		}
		
		private function brunHandler(e:Event):void 
		{
		   music.PlaySound("button");
			parent.dispatchEvent(new eventRun(eventRun.RUN_GAME));		
			//trace("Run");	
		}
////////////////////////////////	
private function hideInterface():void {
            fZone.visible = false;	
		    main_int.visible = false;
			right_menu.visible = false;
			
			//removeIcons();
		}		
////////////////////////////////	
private function viewInterface():void {
	         fZone.visible = true;
			 main_int.visible = true;
			 right_menu.visible = true;
		}//end


		  
	}

}